var usernameFlag = false;
var pwdFlag = false;
var pwdCmt = false;
function mession(obj){
	$(obj).addClass("inputTxtClass-focus");
	$(obj).parent().next("").find("div.first").css("display","block");
	$(obj).parent().next("").find("div.second").css("display","none");
	$(obj).parent().next("").find("div.thread").css("display","none");
}

function validVal(obj){
	$(obj).parent().next("").find("div.first").css("display","none");
	var val = jQuery.trim($(obj).val());
	if($(obj).attr("id")=="username"){
    	var regu = "^[0-9a-zA-Z\\_]{5,20}$";
    	var regu1 = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
    	var regu2 = "^[0-9]{11}$";
    	var re = new RegExp(regu);
    	var re1 = new RegExp(regu1);
    	var re2 = new RegExp(regu2);
    	if(val == ""){
    		$(obj).parent().next("").find("div[class='second'] span").text("请输入用户名");
            $(obj).parent().next("").find("div[class='second']").css("display","block");
            usernameFlag = false;
            return false;
    	}
    	if(re.test(val) || re1.test(val) || re2.test(val)){
            $.ajax({
            	async: false,
         		url : "ValUserName",
         		type : "post",
         		data:"username="+val,
         		dataType:"json",
         		success : function(data) {
         			var bool = data.message;
         			if(bool=="true"){
         				$(obj).parent().next("").find("div[class='thread']").css("display","block");
         				usernameFlag = true;
         			}else{
         	            $(obj).parent().next("").find("div[class='second'] span").text("该用户已经被注册过");
         	            $(obj).parent().next("").find("div[class='second']").css("display","block");	
         	            usernameFlag = false;
         			}
         		}
         	});
    		return usernameFlag;
    	}else{
    		$(obj).parent().next("").find("div[class='second'] span").text("用户名格式输入错误");
            $(obj).parent().next("").find("div[class='second']").css("display","block");
            usernameFlag = false;
            return false;
    	}
	}else if($(obj).attr("id")=="password"){
            var regu = "^[0-9a-zA-Z]{6,16}$";
            var re = new RegExp(regu);
            if(val == ""){
        		$(obj).parent().next("").find("div[class='second'] span").text("请输入密码");
                $(obj).parent().next("").find("div[class='second']").css("display","block");
                pwdFlag = false;
                return false;
            }
            if(re.test(val)){
            	$(obj).parent().next("").find("div[class='thread']").css("display","block");
            	pwdFlag = true;
            	return true;
            }else{
        		$(obj).parent().next("").find("div[class='second'] span").text("密码格式输入错误");
                $(obj).parent().next("").find("div[class='second']").css("display","block");
                pwdFlag = false;
                return false;
            }
	}else{
		if(val == ""){
    		$(obj).parent().next("").find("div[class='second'] span").text("请再次输入确认密码");
            $(obj).parent().next("").find("div[class='second']").css("display","block");
            pwdCmt = false;
            return false;
		}else if(val != $("#password").val()){
    		$(obj).parent().next("").find("div[class='second'] span").text("两次密码输入不一致");
            $(obj).parent().next("").find("div[class='second']").css("display","block");
            pwdCmt = false;
            return false;
		}else{
        	$(obj).parent().next("").find("div[class='thread']").css("display","block");
        	pwdCmt = true;
        	return true;
		}
	}
}

function regist(){
	if(usernameFlag == false){
		validVal($("#username").get(0));
		validVal($("#password").get(0));
		validVal($("#commitPassword").get(0));
	}else{
		validVal($("#password").get(0));
		validVal($("#commitPassword").get(0));
	}
	if(usernameFlag && pwdFlag && pwdCmt){
		var pwd = $("#password").val();
		var pwdArr = MD5Encry(pwd);
		$("#md5password").attr("value",pwdArr[1]);
		$("#encry").attr("value",pwdArr[0]);
		return true;
	}else{
		return false;
	}
}